package com.qtjf.qtmoneyservice.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qtjf.qtmoneyservice.MyApplication;
import com.qtjf.qtmoneyservice.R;
import com.qtjf.qtmoneyservice.adapter.HomeRecyclerAdapter;
import com.qtjf.qtmoneyservice.constantSet.Constant;
import com.qtjf.qtmoneyservice.domain.GouMaiPost;
import com.qtjf.qtmoneyservice.domain.ResultProduct;
import com.qtjf.qtmoneyservice.domain.ResultProductDetail;

import java.util.HashMap;
import java.util.Map;

public class ConfirmJieKuanActivity extends BaseActivity{

    ImageView mBack;
    TextView nextBt;
    TextView jineText;
    TextView shiChangText;
    TextView shenheText;
    TextView guanliText;
    TextView huankuanText;
    TextView daoshouText;
    TextView huankuanText1;
    TextView huanKuanTimeText1;
    ResultProductDetail resultProduct;
    String id="0";
    Gson gson;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_jie_kuan);
        super.setTranslucent();

        Intent intent=getIntent();
        id=intent.getStringExtra("id");

        initView();
        initData();
        initEvent();
    }

    private void initView() {

        gson=new Gson();
        mBack= (ImageView) findViewById(R.id.back_bt);
        nextBt= (TextView) findViewById(R.id.next_bt);
        jineText= (TextView) findViewById(R.id.jine_text);
        shiChangText= (TextView) findViewById(R.id.shichang_text);
        shenheText= (TextView) findViewById(R.id.shenhe_text);
        guanliText= (TextView) findViewById(R.id.guanli_text);
        huankuanText= (TextView) findViewById(R.id.huankuan_text);
        daoshouText= (TextView) findViewById(R.id.daozhang_text);

        huankuanText1= (TextView) findViewById(R.id.huankuan_text1);
        huanKuanTimeText1= (TextView) findViewById(R.id.huankuan_time_text1);
    }



    private void initData() {
        requestCall();
    }



    private void initEvent() {
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ConfirmJieKuanActivity.this.finish();
            }
        });

        nextBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("999",id);
               // startActivity(new Intent(ConfirmJieKuanActivity.this,JiekuaningActivity.class));
                GouMaiPost post=new GouMaiPost(id,"0","1","0","7","234","10","10","10","10");
                gouMai(post);
            }
        });
    }

    private void requestCall() {
        String url= Constant.HOMEADDRESS + "products/getProductInfo?id="+id+"&"+"usermobile="+Constant.getUsermobile();
        final StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                // TODO Auto-generated method stub
                Log.d("999",response);
                if(response.length()>25) {
                    resultProduct = gson.fromJson(response, new TypeToken<ResultProductDetail>() {
                    }.getType());
                    if(resultProduct!=null)
                    {
                        jineText.setText(resultProduct.getData().getAmount());
                        shiChangText.setText(resultProduct.getData().getDaynum());
                        shenheText.setText(resultProduct.getData().getAuditCost());
                        guanliText.setText(resultProduct.getData().getManageCost());
                        daoshouText.setText("实际到账￥"+resultProduct.getData().getArriveAmount());
                        if(resultProduct.getData().getInstalments()!=null&&resultProduct.getData().getInstalments().size()>0) {
                            huankuanText.setText("￥" + resultProduct.getData().getInstalments().get(0).getAmount());
                            huankuanText1.setText("￥" + resultProduct.getData().getInstalments().get(0).getAmount());
                            huanKuanTimeText1.setText(resultProduct.getData().getInstalments().get(0).getRepayAt());
                        }
                    }

                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                Toast.makeText(ConfirmJieKuanActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
            }

        }) {
            // 需要重写获取参数的函数,可以向服务器提交参数
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                return map;
            }

            //设置请求头
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {

                Map<String, String> headers = new HashMap<String, String>();
                headers.put("Authorization", "");
                return headers;
            }

        };


        MyApplication.getQueue().add(stringRequest);

    }



    private void gouMai(final GouMaiPost post) {
        String url= Constant.HOMEADDRESS + "borrowmoney/insetBorrowMoney";
        final StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                // TODO Auto-generated method stub
                Log.d("999",response);
         /*       if(response.length()>25) {
                    resultProduct = gson.fromJson(response, new TypeToken<ResultProductDetail>() {
                    }.getType());
                    if(resultProduct!=null)
                    {
                        jineText.setText(resultProduct.getData().getAmount());
                        shiChangText.setText(resultProduct.getData().getDaynum());
                        shenheText.setText(resultProduct.getData().getAuditCost());
                        guanliText.setText(resultProduct.getData().getManageCost());
                        daoshouText.setText("实际到账￥"+resultProduct.getData().getArriveAmount());
                        if(resultProduct.getData().getInstalments()!=null&&resultProduct.getData().getInstalments().size()>0) {
                            huankuanText.setText("￥" + resultProduct.getData().getInstalments().get(0).getAmount());
                            huankuanText1.setText("￥" + resultProduct.getData().getInstalments().get(0).getAmount());
                            huanKuanTimeText1.setText(resultProduct.getData().getInstalments().get(0).getRepayAt());
                        }
                    }

                }*/
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                Toast.makeText(ConfirmJieKuanActivity.this, "请检查网络", Toast.LENGTH_SHORT).show();
            }

        }) {
            // 需要重写获取参数的函数,可以向服务器提交参数
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("data",gson.toJson(post));
                return map;
            }

            //设置请求头
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {

                Map<String, String> headers = new HashMap<String, String>();
                headers.put("Authorization", "");
                return headers;
            }

        };


        MyApplication.getQueue().add(stringRequest);

    }
}
